import axios from "axios";
import config from "@/util/config.js";

//创建一个axios的对象
const unitAPI = {
    mdBaseUrl: config.mdUrl+"/md",
    mdProMat: "/unit",
    timeout: 5000,
    //获取所有单位信息
    getALl(param, callback) {
        axios.get(this.mdBaseUrl + this.mdProMat + "/query/" + param.page + "/" + param.limit).then(res => {
            callback(res.data);
        })
    },
    //不含分页的获取单位信息
    getAllUnit(callback) {
        axios.get(this.mdBaseUrl + this.mdProMat + "/queryAll").then(res => {
            callback(res.data);
        })
    },
    //根据名称模糊查询单位信息
    findByName(param, callback) {
        var queryPath = "";
        if (param.name === "") {
            queryPath = "/query/" + param.page + "/" + param.limit;
        } else {
            queryPath = "/query/" + param.name + "/" + param.page + "/" + param.limit;
        }
        axios.get(this.mdBaseUrl + this.mdProMat + queryPath).then(res => {
            callback(res.data);
        });
    },
    //添加单位信息
    add(param, callback) {
        axios.request(
            {
                url: this.mdBaseUrl + this.mdProMat + "/add",
                method: "post",
                data: param,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8"
                },
                timeout: this.timeout,
            }
        ).then((res) => {
            callback(res.data);
        })
    },
    //删除单位信息（需改状态为0）
    delete(param, callback) {
        axios.request(
            {
                url: this.mdBaseUrl + this.mdProMat + "/update/status",
                method: "put",
                timeout: this.timeout,
                params: param
            }).then((res) => {
            callback(res.data)
        })
    },
    //修改单位信息
    update(param, callback) {
        axios.request(
            {
                url: this.mdBaseUrl + this.mdProMat + "/update",
                method: "put",
                timeout: this.timeout,
                data: param
            }).then((res) => {
            callback(res.data)
        })
    },
    //获取数量
    getCount(callback) {
        axios.get(this.mdBaseUrl + this.mdProMat + "/query/count").then(res => {
            callback(res.data);
        })

    }
}
export default unitAPI;